var prefix = "/sys/menu";
$(document).ready(function () {
    load();
});
showButton('#menu');
var load = function () {
    $('#exampleTable')
        .bootstrapTreeTable(
            {
                id: 'menuId',
                code: 'menuId',
                parentCode: 'parentId',
                type: "post", // 请求数据的ajax类型
                url: API_SERVER_URL + "sys/menu/list", // 请求数据的ajax的url
                ajaxParams: JSON.stringify({sort:'order_num'}), // 请求数据的ajax的data属性
                expandColumn: '0',// 在哪一列上面显示展开按钮
                striped: true, // 是否各行渐变色
                bordered: true, // 是否显示边框
                expandAll: false, // 是否全部展开
                // toolbar : '#exampleToolbar',
                columns: [
                   {
                       title: '名称',
                       field: 'name',
                       valign: 'center',
                       width:'280px'
                   },
                   {
                       title: '图标',
                       field: 'icon',
                       align: 'center',
                       valign: 'center',
                       width : '60px',
                       formatter: function (item, index) {
                           return item.icon == null ? ''
                               : '<i class="' + item.icon
                               + ' fa-lg"></i>';
                       }
                   },
                   {
                       title: '类型',
                       field: 'type',
                       align: 'center',
                       valign: 'center',
                       width : '100px',
                       formatter: function (item, index) {
                           if (item.type === 0) {
                               return '<span class="label label-primary">目录</span>';
                           }
                           if (item.type === 1) {
                               return '<span class="label label-success">菜单</span>';
                           }
                           if (item.type === 2) {
                               return '<span class="label label-warning">按钮</span>';
                           }
                       }
                   },
                   {
                       title: '地址',
                       valign: 'center',
                       field: 'url'
                   },
                   {
                       title: '权限标识',
                       valign: 'center',
                       field: 'perms',
                       width:'250px'
                   },
                   {
                       title: '操作',
                       field: 'id',
                       align: 'center',
                       valign: 'center',
                       width:'160px',
                       formatter: function (item, index) {
                           var e = '<a class="btn warning btn-sm '
                               + s_edit_h
                               + '" href="javascript:void()0;"  title="编辑" onclick="edit(\''
                               + item.menuId
                               + '\')"><img src= "'+SERVER_URL+'static/img/edit.png"></a> ';
                           var p = '<a class="btn warning btn-sm '
                               + s_add_h
                               + '" href="javascript:void()0;"  title="添加下级" onclick="add(\''
                               + item.menuId
                               + '\')"><img src= "'+SERVER_URL+'static/img/add.png"></a> ';
                           var d = '<a class="btn warning btn-sm '
                               + s_remove_h
                               + '" href="javascript:void()0;" title="删除"   onclick="remove(\''
                               + item.menuId
                               + '\')"><img src= "'+SERVER_URL+'static/img/remove.png"></a> ';
                          var permissionsArr = JSON.parse(localStorage.getItem("permissions"));
						
						   if(permissionsArr.indexOf('sys:menu:edit') == -1){
						    	e = '';
						   }
						
						   if(permissionsArr.indexOf('sys:menu:remove') == -1){
						  		d = '';
						   }
						
						   if(permissionsArr.indexOf('sys:menu:add') == -1){
						  		p = '';
						   }
							return e+d+p;
                       }
                   }]
           });
}

function reLoad() {
    load();
}

function add(pId) {
    layer.open({
        type: 2,
        title: '增加菜单',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: '../../../templates/system/menu/add.html',/*prefix + '/add/' + pId */// iframe的url
        success: function(layero, index){
             $.ajax({
                type : 'get',
                headers:PRIVATE_HEADERS,
                url :API_SERVER_URL + 'sys/menu/getById/'+pId,
                success : function(r) {

                    if(r==''){
                    var body = layer.getChildFrame('body',index);//建立父子联系
                      var iframeWin = window[layero.find('iframe')[0]['name']];
                    // console.log(arr); //得到iframe页的body内容
                    // console.log(body.find('input'));
                    var inputList = body.find('input');
                        $(inputList[1]).val('顶级部门');
                    }
                    else{
                        parentId = r.parentId;
                        name = r.name;
                        menuId = r.menuId;
                        var body = layer.getChildFrame('body',index);//建立父子联系
                          var iframeWin = window[layero.find('iframe')[0]['name']];
                        // console.log(arr); //得到iframe页的body内容
                        // console.log(body.find('input'));
                        var inputList = body.find('input');
                        /*if(delFlag == '0'){
                            $(inputList[5]).attr("checked",true);
                        }
                        else{
                            $(inputList[5]).attr("checked",false);
                        }*/
                        $(inputList[0]).val(menuId);
                            if(parentId==0){
                                $(inputList[1]).val(name);
                            }
                            else{
                                $(inputList[1]).val(name);
                            }
                    }
                }
             });
        }
    });
}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            headers:PRIVATE_HEADERS,
            url: API_SERVER_URL + "sys/menu/remove",
            type: "post",
            data: {
                'id': id
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("删除成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function edit(id) {
var _this = $(this),
        data =_this.parent().siblings(),
        arr = id;
    layer.open({
        type: 2,
        title: '菜单修改',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: '../../../templates/system/menu/edit.html', // iframe的url
        		 success: function(layero, index){
        		             $.ajax({
                                			type : 'get',
                                			headers:PRIVATE_HEADERS,
                                			url :API_SERVER_URL + 'sys/menu/getById/'+id,
                                			success : function(r) {
                                			    menuId = r.menuId;
                                				parentId = r.parentId;
                                                name = r.name;
                                				orderNum = r.orderNum;
                                				delFlag = r.delFlag;
                                				url = r.url;
                                				perms = r.perms;
                                				orderNum = r.orderNum;
                                				type = r.type;
                                				icon = r.icon;
                                				var body = layer.getChildFrame('body',index);//建立父子联系
                                                var iframeWin = window[layero.find('iframe')[0]['name']];
                                                // console.log(arr); //得到iframe页的body内容
                                                // console.log(body.find('input'));
                                                var inputList = body.find('input');

                                                    if(parentId =='0'){
                                                        $(inputList[3]).val('顶级菜单');
                                                    }
                                                    else{
                                                        $.ajax({
                                                            type : 'get',
                                                            headers:PRIVATE_HEADERS,
                                                            url :API_SERVER_URL + 'sys/menu/getById/'+parentId,
                                                            success : function(y) {
                                                                  var names = y.name;
                                                                  var inputList = body.find('input');
                                                                  $(inputList[3]).val(names);
                                                                }
                                                            })

                                                    }
                                                    if (type === 0) {
                                                         $(inputList[4]).attr("checked",true);
                                                     }
                                                    if (type === 1) {
                                                         $(inputList[5]).attr("checked",true);
                                                     }
                                                    if (type === 2) {
                                                         $(inputList[6]).attr("checked",true);
                                                     }
                                                    /*if(type == null){
                                                         $(inputList[1]).val(name);
                                                    }*/
                                                    $(inputList[1]).val(menuId);
                                                    $(inputList[7]).val(name);
                                                    $(inputList[8]).val(url);
                                                    $(inputList[9]).val(perms);
                                                    $(inputList[10]).val(orderNum);
                                                    $(inputList[11]).val(icon);
                                			}
                                		});



                                 }
    });
}

function batchRemove() {
    // var rows = $('#exampleTable').bootstrapTable('getSelections');

}